package itsm.isperp.module.controller.incident;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.domain.CountQuery;
import itsm.isperp.framework.web.request.DefaultCountQuery;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.service.incident.IcdIncidentService;
import itsm.isperp.workflow.utils.BpmConst;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("incident/main")
public class IcdIncidentMainController {

	@Autowired
	protected IcdIncidentService icdIncidentService;

	private CountQuery createDefaultCountQuery() {
		CountQuery cq = new DefaultCountQuery("icd_incident");
		return cq;
	}

	protected PageResponse<Map<String, Object>> getListDetail(CountQuery cq,
			JqGridRequest request, HttpServletRequest req) {
		String queryPath = req.getParameter("queryPath");

		if (cq != null) {
			request.setCountQuery(cq);
			request.setQueryPath(queryPath);

		}
		return icdIncidentService.findAllDetailByRequest(request).toPage();
	}

	protected CountQuery createServiceDeskCountQuery(String team) {
		CountQuery cq = this.createDefaultCountQuery();

		String condition = "";
		if (!"1".equals(team)) {
			condition = " AND assigner_id='"
					+ ContextHolder.getLoginUsername() + "'";
		} else {
			condition = "  AND  status<>'新建'";
		}

		cq.addCountFilter("未关闭", true, " status<>'已关闭' AND  status<>'待分派' "
				+ condition);

		cq.addCountFilter("VIP", true, "  status<>'已关闭'  AND  are_vip=1 "
				+ condition);

		cq.addCountFilter("SLA超期", true,
				" sla_status=1 AND  status<>'已关闭' " + condition);
		cq.addCountFilter("重大", true,
				" priority_name='一级' AND   status<>'已关闭' " + condition);

		cq.addCountFilter("不满意", true,
				"  satisfaction IN (2,1)  AND feedback_status IS NULL "
						+ condition);

		cq.addCountFilter(
				"多次催办",
				true,
				" status<>'已关闭' AND EXISTS (SELECT source_id FROM app_message_success WHERE  MESSAGE_LEVEL=1 AND source_id=id)  "
						+ condition);

		cq.addCountFilter("挂起", true, " status IN ('挂起','挂起审核','挂起中') "
				+ condition);
		cq.addCountFilter("已关闭", true, " status='已关闭' "
				+ condition);

		return cq;
	}

	@RequestMapping("serviceDeskManage")
	public ModelAndView serviceDeskManage(String team) {
		ModelAndView mv = new ModelAndView();
		if (team == null) {
			team = "0";
		}
		mv.addObject("team", team);
		mv.setViewName("incident/main/serviceDeskManage");

		return mv;

	}

	@RequestMapping("serviceDeskMenu")
	@ResponseBody
	public ITreeNode[] serviceDeskMenu(String team) {
		return this.createServiceDeskCountQuery(team).getTreeNodes();

	}

	@RequestMapping("serviceDeskList")
	@ResponseBody
	public PageResponse<Map<String, Object>> serviceDeskList(
			JqGridRequest request, HttpServletRequest req) {
		String team = req.getParameter("team");
		return this.getListDetail(createServiceDeskCountQuery(team), request,
				req);
	}

	// ==============================新事件===========================

	@RequestMapping("serviceDeskNewIncidentMenu")
	@ResponseBody
	public ITreeNode[] serviceDeskNewIncidentMenu() {
		return this.createServiceDeskNewIncidentCountQuery().getTreeNodes();

	}

	@RequestMapping("serviceDeskNewIncidentList")
	@ResponseBody
	public PageResponse<Map<String, Object>> serviceDeskNewIncidentList(
			JqGridRequest request, HttpServletRequest req) {
		return this.getListDetail(createServiceDeskNewIncidentCountQuery(),
				request, req);
	}

	@RequestMapping("serviceDeskNewIncidentCount")
	@ResponseBody
	public Long serviceDeskNewIncidentCount() {

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		String countSql="select count(1) from ("
				+ " SELECT ID FROM icd_incident a WHERE deal_type is null  and "
				+ BpmConst
						.getQueryMyTaskSqlCondition("incidentProcess")
				+ ")T";

		return jdbcTemplate
				.queryForObject(countSql, Long.class);
	}

	protected CountQuery createServiceDeskNewIncidentCountQuery() {
		CountQuery cq = this.createDefaultCountQuery();

		cq.addCountFilter(
				"Web",
				true,
				"  (source='Web' AND status='待分派' AND "
						+ BpmConst
								.getQueryMyTaskSqlCondition("incidentProcess")
						+ ")");
		cq.addCountFilter(
				"微信",
				true,
				"  (source='微信' AND status='待分派' AND "
						+ BpmConst
								.getQueryMyTaskSqlCondition("incidentProcess")
						+ ")");
		cq.addCountFilter("监控系统", true,
				"   (status<>'已关闭' and status<>'新建') AND source ='监控系统'   ");
		
		

		cq.addCountFilter(
				"其它",
				true,
				"  source NOT IN('Web','监控系统','微信') AND  status='待分派' AND "
						+ BpmConst
								.getQueryMyTaskSqlCondition("incidentProcess"));
		cq.addCountFilter(
				"待处理",
				true,
				"  status<>'待分派'  AND "
						+ BpmConst
								.getQueryMyTaskSqlCondition("incidentProcess"));
		cq.addCountFilter("草稿",
        new StringBuffer(" ( process_id IS NULL AND  (creator ='")
            .append(ContextHolder.getLoginUsername()).append("'")
            .toString()
            + "  or creator in (select app_user_name from v_app_user_role where name='服务台')) and status<>'已关闭')");

		return cq;
	}

	@RequestMapping("serviceDeskNewIncidentManage")
	public ModelAndView serviceDeskNewIncidentManage() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("incident/main/serviceDeskNewIncidentManage");

		return mv;

	}

}
